package com.test.jbpm.web.action;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.test.jbpm.Pager;
import com.test.jbpm.domain.Student;
import com.test.jbpm.domain.User;
import com.test.jbpm.service.StudentService;




@Controller
@Scope("prototype")
public class StudentAction extends BaseAction {

	private StudentService studentService;
	
	private User user;
	private Student student;
	
	private Integer[] studentId;
	

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}



	public Integer[] getStudentId() {
		return studentId;
	}

	public void setStudentId(Integer[] studentId) {
		this.studentId = studentId;
	}
	@Autowired
	public void setStudentService(StudentService studentService) {
		this.studentService = studentService;
	}

	public void setStudent(Student student) {
		this.student = student;
	}

	public Student getStudent() {
		return student;
	}

	public String index() throws Exception {
		return SUCCESS;
	}
	
	/**
	 * 返回JSON字符串
	 * {total:100, data:[{},{},{}]}
	 * @return
	 * @throws Exception
	 */
	public String list() throws Exception {
		Pager<Student> pager = studentService.findStudentList();
		//{total:100, data:[{},{},{}]}
		StringBuffer sbJson = new StringBuffer();
		sbJson.append("{");
		sbJson.append("total:");
		sbJson.append(pager.getTotal());
		sbJson.append(", data:[");
		List<Student> studentList = pager.getData();
		for (int i=0; i<studentList.size(); i++) {
			Student student = studentList.get(i);
			sbJson.append(student2JsonString(student));
			if (i<(studentList.size() - 1)) {
				sbJson.append(",");
			}
		}
		sbJson.append("]");
		sbJson.append("}");
		System.out.println(sbJson.toString());
		return ajaxOut(sbJson.toString());
	}
	
	public String addOrUpdate() throws Exception {
		studentService.addOrUpdate(student);
		return ajaxOut("{success: true}");
	} 	
	
	public String edit() throws Exception {
		student = studentService.findStudentById(student.getId());
		return ajaxOut("{success:true, data:" + student2JsonString(student) + "}");
	}
	
	private String student2JsonString(Student student) {
		StringBuilder sbJson = new StringBuilder();
		sbJson.append("{");
		sbJson.append("id:");
		sbJson.append(student.getId());	
		sbJson.append(",name:'");
		sbJson.append(student.getName());
		sbJson.append("',sex:'");
		sbJson.append(student.getSex());
		sbJson.append("',age:'");
		sbJson.append(student.getAge());
		sbJson.append("',firstInterviewScore:'");
		sbJson.append(student.getFirstInterviewScore());
		sbJson.append("',secondInterviewScore:'");
		sbJson.append(student.getSecondInterviewScore());
		sbJson.append("',thirdInterviewScore:'");
		sbJson.append(student.getThirdInterviewScore());
		sbJson.append("',photo:'");
		sbJson.append(student.getPhoto());
		sbJson.append("',email:'");
		sbJson.append(student.getEmail());
		sbJson.append("'}");
		return sbJson.toString();
	}
	
	public String del() throws Exception {
		studentService.del(studentId);
		return ajaxOut();
	}
	
	
	
	
}
